#include "StdAfx.h"
#include "Player.h"
#include <iostream>
#include "KeyCommon.h"
#include "PacketStruct.h"

CPlayer::CPlayer(void):
m_pPacketInfo(new struct Packet())
{
	m_pPacketInfo->iID = 10;
	m_pPacketInfo->X = 100;
	m_pPacketInfo->y = 100;

	rPlayer.left = 100;
	rPlayer.top = 100;
	rPlayer.right = 200;
	rPlayer.bottom = 200;
}


CPlayer::~CPlayer(void)
{
	if(nullptr != m_pPacketInfo)
		delete m_pPacketInfo;
}

void CPlayer::Init(HDC hdcBackBuffer)
{
	m_hdcBackBuffer = hdcBackBuffer;
}


void CPlayer::Progress()
{
}

void CPlayer::Render()
{
	Rectangle(m_hdcBackBuffer, rPlayer.left,
		rPlayer.top,
		rPlayer.right,
		rPlayer.bottom);

	/*
	RECT rTestRect;
	rTestRect.left = 300;
	rTestRect.top = 300;
	rTestRect.right = 400;
	rTestRect.bottom = 400;

	Rectangle(m_hdcBackBuffer, rTestRect.left, rTestRect.top, rTestRect.right, rTestRect.bottom);

	RECT rResult;
	if(true == IntersectRect(&rResult, &rTestRect, &rPlayer))
	{
		printf("Collision");
	}
	*/
}

void CPlayer::OnEvent(int iEvent)
{
	if(LEFT == iEvent)
		MoveLeft();
	else if(RIGHT == iEvent)
		MoveRight();
	else if(UP == iEvent)
		MoveUp();
	else if(DOWN == iEvent)
		MoveDown();
}


void CPlayer::MoveLeft()
{
	if(0 >= rPlayer.left)
		return;
	
	m_pPacketInfo->X -= 10;
	rPlayer.left = m_pPacketInfo->X;
	rPlayer.right = m_pPacketInfo->X + 100;		
}

void CPlayer::MoveRight()
{
	if(rPlayer.right >= 1024)
		return;

	m_pPacketInfo->X += 10;

	rPlayer.left = m_pPacketInfo->X;
	rPlayer.right = m_pPacketInfo->X + 100;
}

void CPlayer::MoveUp()
{
	if(0 >= rPlayer.top)
		return;

	m_pPacketInfo->y -= 10;

	rPlayer.top = m_pPacketInfo->y;
	rPlayer.bottom = m_pPacketInfo->y + 100;
}

void CPlayer::MoveDown()
{
	if(rPlayer.bottom >= 768)
		return;
	
	m_pPacketInfo->y += 10;

	rPlayer.top = m_pPacketInfo->y;
	rPlayer.bottom = m_pPacketInfo->y + 100;
}
